<?php

use Illuminate\Database\Capsule\Manager as DB;

#[推客管理]
class TuikeController extends McommonController
{
    public function init() {
        $this->model = (new tuikeModel());
        $this->table = 'tuike';
        $this->primaryKey = 'tuike_id';
        parent::init();
    }

    #[推客分佣记录]
    public function rewardAction(){
        if($this->isPost()){
            $params = $this->postData;
            $page = $params['page'] ?? 1;
            $per_page = $params['rows'] ?? 10;

            $rewardModel = (new tuikerewardModel())->newQuery();
            $query = $rewardModel->with(['tuike','buyerInfo','orderInfo'=>function($withOrderQuery){
                $withOrderQuery->where('dianpu_id', $this->auth->dianpu_id);
            },'orderItemInfo', 'tuikeLevelInfo'])->whereHas('orderInfo', function ($withOrderQuery){
                $withOrderQuery->where('dianpu_id', $this->auth->dianpu_id);
            })->orderByDesc('created_at');
            if(isset($params['mode']) && $params['mode'] != 'all'){
                switch ($params['mode']){
                    case 'waiting':
                        $query = $query->where('status', tuikerewardModel::COMMISSION_REWARD_STATUS_WAITING);
                        break;
                    case 'accounted':
                        $query = $query->where('status', tuikerewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED);
                        break;
                    case 'cancel':
                        $query = $query->where('status', tuikerewardModel::COMMISSION_REWARD_STATUS_CANCEL);
                        break;
                    case 'back':
                        $query = $query->where('status', tuikerewardModel::COMMISSION_REWARD_STATUS_BACK);
                        break;
                }
            }
            if(isset($params['start_on']) && !empty($params['start_on'])){
                $query= $query->whereDate('created_at','>=', $params['start_on']);
            }
            if(isset($params['end_on']) && !empty($params['end_on'])){
                $query= $query->whereDate('created_at', '<=', $params['end_on']);
            }
            if(isset($params['keywords']) && !empty($params['keywords'])){
                if(isset($params['keywordsType']) && !empty($params['keywordsType'])){
                    switch ($params['keywordsType']){
                        case 'tuike_id':
                            $query = $query->where('tuike_id', $params['keywords']);
                            break;
                        case 'tuike_phone':
                            $query = $query->whereIn('tuike_id', memberModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            break;
                        case 'order_id':
                            $query = $query->where('order_id', $params['keywords']);
                            break;
                        case 'order_sn':
                            $query = $query->whereIn('order_id', orderModel::query()->where('order_sn', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            break;
                        case 'buyer_id':
                            $query = $query->where('buyer_id', $params['keywords']);
                            break;
                        case 'buyer_phone':
                            $query = $query->whereIn('buyer_id', memberModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            break;
                        case 'goods_id':
                            $query = $query->whereIn('order_item_id', orderitemModel::query()->where('goods_id', $params['keywords'])->pluck('id')->toArray());
                            break;
                        case 'goods_title':
                            $query = $query->whereIn('order_item_id', orderitemModel::query()->where('goods_title', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            break;
                    }
                }
            }
            $total = $query->count();
            if(isset($params['sort'])){
                $query = $query->orderBy($params['sort']?:'id', (isset($params['order'])&&in_array($params['order'],['asc','desc']))?$params['order']:'desc');
            }
            $data = $query->offset(($page - 1) * $per_page)
                ->limit($per_page)
                ->get()->toArray();
            json([
                'total' => $total,
                'rows'=>$data
            ]);
        }else{
            $this->_view->display( strtolower($this->controller) . '/reward.html');
        }
    }
}